<?php
/**
 * @author spenceryg
 * @date 2022/2/7
 * @time 7:46 上午
 * @focus love & life
 */

namespace app\admin\controller\sys;

use app\admin\controller\sys\business\AdminAccessBusiness;
use app\admin\controller\sys\business\AdminUserBusiness;
use app\admin\controller\sys\model\AdminAccessModel;
use app\admin\controller\sys\model\AdminRoleAccessModel;
use app\admin\controller\sys\validate\AdminAccessValidate;
use app\common\lib\SpClassMethods;
use ReflectionException;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\View;
use think\response\Json;

/**
 * 后台权限
 * Class AdminAccess
 * @package app\admin\controller\sys
 */
class AdminAccess extends \app\BaseController
{
    /**
     * 列表
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): string
    {
        View::assign('data', AdminAccessModel::select());
        return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_access/index.html');
    }

    /**
     * 新增
     * @return string|Json
     * @throws ReflectionException
     */
    public function add()
    {
        if (request()->isPost()) {
            AdminAccessBusiness::add(input());
            return success('添加成功！');
        } else {
            $nodes = AdminAccessBusiness::getAllPublicAction();
            foreach ($nodes as $i => $node) {
                if($node['pid']){
                    $nodes[$i]['id'] = strtolower($node['id']);
                }
            }
            View::assign('nodes', $nodes);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_access/add.html');
        }
    }

    /**
     * 编辑
     * @return string|Json
     * @throws ReflectionException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function update()
    {
        if (request()->isPost()) {
            AdminAccessBusiness::update(input());
            return success('更新成功！');
        } else {
            $id = input('id', 0);
            $r = AdminAccessModel::find($id)->getData();
            View::assign('r', $r);
            $nodes = AdminAccessBusiness::getAllPublicAction();
            foreach ($nodes as $i => $node) {
                if($node['pid']){
                    $nodes[$i]['id'] = strtolower($node['id']);
                }
            }
            foreach ($nodes as $k=>$node) {
                if (strpos($r['path'], '|'. $node['id'] . '|') !== false) {
                    $nodes[$k]['checked'] = true;
                }
            }
            View::assign('nodes', $nodes);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_access/update.html');
        }
    }

    /**
     * 删除
     * @return Json
     */
    public function delete(): Json
    {
        try {
            $id = input('id', 0);
            $adminAccess = AdminAccessModel::find($id);
            $adminAccess->delete();
            AdminRoleAccessModel::where('admin_access_id', $id)->delete();
            return success('删除权限成功！');
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
    }
}
